<?php
/*
 * 前台车系显示类
 * @package    auto
 * @author     YuNian, mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 40dc7f8a2d5b1a4e1fd0bbd8d92b7677c3fbbcd3 $
 */
defined('IN_IXDCW') or exit('Access Denied');
/**
 * 车系显示类
 * Class AutoShowCar
 */
class AutoCar {

	private static $_tableCategory;
	private static $_tableCar;

	private static function _getTableCategory() {
		if (!empty(self::$_tableCategory)) {
			return self::$_tableCategory;
		} else {
			self::$_tableCategory = Table::name(Table::NAME_AUTO_CATEGORY);
			return self::$_tableCategory;
		}
	}

	private static function _getTableCar() {
		if (!empty(self::$_tableCar)) {
			return self::$_tableCar;
		} else {
			self::$_tableCar = Table::name(Table::NAME_AUTO_RANK_CAR);
			return self::$_tableCar;
		}
	}

	/**
	 * 获取车系信息
	 * @param       $carId
	 * @param array $condition
	 * @param int   $limit
	 * @return array|null
	 */
	public static function get($carId, $condition = array(), $limit = 0) {
		global $db;
		$extSql = '';
		if ($condition) {
			foreach ($condition as $ck => $cv) {
				$extSql .= ' and ' . $ck . $cv;
			}
		}
		$carIdin = is_numeric($carId) ? intval($carId) : implode(',', $carId);
		$sql     = "select * from  " . self::_getTableCar() . " tc right join " . self::_getTableCategory() . " c on  c.arid = tc.arid where c.arid in($carIdin) $extSql";
		if ($limit) {
			$sql .= ' limit ' . $limit;
		}
		if (is_numeric($carId)) {
			return $db->getOne($sql);
		} else {
			return $db->getAll($sql);
		}
	}


	/**
	 * 获取简短车系信息
	 * @param $carId
	 * @return array|null
	 */
	public static function getShort($carId) {
		global $db;
		$carIdin = is_numeric($carId) ? intval($carId) : implode(',', $carId);
		$sql     = "select * from  " . self::_getTableCategory() . " where arid in($carIdin)";
		if (is_numeric($carId)) {
			return $db->getOne($sql);
		} else {
			$full = array();
			$rs = $db->query($sql);
			while($r = $db->fetchArray($rs)) {
				$full[$r['arid']] = $r;
			}
			return $full;
		}
	}

	/**
	 * 获取品牌的完整信息
	 * @param $carId
	 * @return array|null
	 */
	public static function getFull($carId) {
		global $db;
		$carIdin = is_numeric($carId) ? intval($carId) : implode(',', $carId);
		$sql     = "select * from  " . self::_getTableCar() . " tb, " . self::_getTableCategory() . " c where c.arid = tb.arid and c.arid in($carIdin)";
		if (is_numeric($carId)) {
			return $db->getOne($sql);
		} else {
			return $db->getAll($sql);
		}
	}

	/**
	 * 获取品牌的ID
	 * @param $carId
	 * @return mixed
	 */
	public static function getBrandId($carId) {
		global $db;
		$sql  = "select arrparentid from  " . self::_getTableCategory() . " where arid = $carId";
		$pids = explode(',', $db->getVar($sql, 'arrparentid'));
		if (!$pids[1]) {
			return 0;
		}
		return $pids[1];
	}

	/**
	 * 获取生产商
	 * @param $carId
	 * @return mixed
	 */
	public static function getManufactorId($carId) {
		global $db;
		$sql = "select parentid from  " . self::_getTableCategory() . " where arid = $carId";
		$mid = $db->getVar($sql, 'parentid');
		if ($mid) {
			return $mid;
		} else {
			return 0;
		}
	}

	/**
	 * 获取该品牌下所有车系
	 * @param     $carid
	 * @param int $limit
	 * @return array
	 */
	public static function relation($carid, $limit = 8) {
		$sibings = self::siblings($carid, 0);
		$sibings = is_array($sibings) ? $sibings : array($sibings);
		return self::get($sibings, array(), $limit);
	}

	/**
	 * 获取关注
	 * @param     $carid
	 * @param int $limit
	 * @return array|null
	 */
	public static function attention($carid, $limit = 8) {
		$sibings = self::siblings($carid, 0);
		$sibings = is_array($sibings) ? $sibings : array($sibings);
		return self::get($sibings, array('c.attention' => '>=0'), $limit);
	}

	/**
	 * 简单更新点击量
	 * @param $carid
	 * @return array|bool|mixed|mysqli_result|string
	 */
	public static function updateHits($carid) {
		global $db;
		$sql = Sql::update(self::_getTableCategory(), 'hits=hits+1', "arid=$carid");
		return $db->query($sql);
	}

	/**
	 * 兄弟元素
	 * @param     $carid
	 * @param int $limit
	 * @return array
	 */
	public static function siblings($carid, $limit = 8) {
		global $db;
		$brandId = self::getBrandId($carid);
		$sql     =
			"select arid from " . self::_getTableCategory() . "
			where
				parentid in (select arid from " . self::_getTableCategory() . " where parentid =$brandId)
				and arid != $carid
			order by edittime desc";
		if ($limit) {
			$sql .= " limit $limit";
		}
		return $db->getCol($sql, 'arid');
	}

	/**
	 * 从数据库中获取列表
	 * @param        $filter
	 * @param string $fields
	 * @return array
	 */
	public static function getList($filter, $fields = '*') {
		global $db;
		$tableAutoModel    = Table::name(Table::NAME_AUTO_MODEL);
		$tableAutoCategory = Table::name(Table::NAME_AUTO_CATEGORY);
		$condition         = ' 1 ';
		if ($filter['acid']) { // 级别
			$condition .= " and acid in (" . AutoCategoryLevel::get($filter['acid'], 'arrchildid') . ")";
		}
		if ($filter['arbid']) { // 级别
			$condition .= " and arbid = " . $filter['arbid'];
		}
		if (isset($filter['engine'])) { // level
			$acid = AutoCategory::getCacheItem(AutoCategory::TYPE_ENGINE, $filter['engine'], 'arrchildid');
			$condition .= ' and enginetype in(' . $acid . ') ';
		}
		$sql           = "select distinct(arcid) arcid from {$tableAutoModel} where $condition";
		$distinctCarid = $db->getCol($sql, 'arcid');
		if ($distinctCarid) {
			$sql  = "select $fields from {$tableAutoCategory} where arid in (" . implode(',', $distinctCarid) . ")";
			$list = array();
			$rs   = $db->query($sql);
			while ($r = $db->fetchArray($rs)) {
				$list[$r['arid']] = $r;
			}
			return $list;
		} else {
			return array();
		}
	}

}